home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / FLI106C.ZIP;1 / DIAMULTI.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-12  |  10.6 KB  |  500 lines

  1. //
  2. // The Fusion Library Interface for DOS
  3. // Version 1.06c
  4. // Copyright (C) 1990, 1991, 1992
  5. // Software Dimensions
  6. //
  7. // DialogClass
  8. // DiaMulti - MLEE (Multi Line Edit Element, pronouced Millie)
  9. //
  10.  
  11. #include "fli.h"
  12. #include "elements.h"
  13. #include "colors.h"
  14.  
  15. #ifdef __BCPLUSPLUS__
  16. #pragma hdrstop
  17. #endif
  18.  
  19. #include <alloc.h>
  20. #include <ctype.h>
  21. #include <string.h>
  22. #include <dos.h>
  23. #include <mem.h>
  24.  
  25. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  26. //
  27. // DiaMulti()
  28. //
  29. // Constructor
  30. //
  31. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  32.  
  33. DiaMulti::DiaMulti(int _X,int _Y,int _Width,int _Height,char *Storage)
  34. {
  35.   X=_X;
  36.   Y=_Y;
  37.   Width=_Width;
  38.   Height=_Height;
  39.  
  40.   CurX=0;
  41.   CurY=0;
  42.   Relative=0;
  43.  
  44.   MaxWidth=Width*Height;
  45.  
  46.   String=Storage;
  47. }
  48.  
  49. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  50. //
  51. // Triangulate()
  52. //
  53. // Locates the start and end of a particular line.  This will feed the
  54. // Start and End variable members of the DiaMulti class.
  55. //
  56. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  57.  
  58. void DiaMulti::Triangulate(int Line)
  59. {
  60.   Start=0;
  61.   End=0;
  62.  
  63.   int OutputY=0;
  64.   int EndOfLastLine=0;
  65.  
  66.   if (!strlen(String))
  67.     return;
  68.   else
  69.   {
  70.     char *String=DiaMulti::String;
  71.  
  72.     int StrWidth=strlen(String);
  73.  
  74.     do
  75.     {
  76.       if (Width>StrWidth)
  77.       {
  78.         if (Line==OutputY)
  79.         {
  80.           Start=EndOfLastLine;
  81.           End=EndOfLastLine+strlen(String+EndOfLastLine);
  82.           break;
  83.         }
  84.  
  85.         OutputY++;
  86.         StrWidth=-1;
  87.  
  88.         break;
  89.       }
  90.       else
  91.       {
  92.         int i;
  93.  
  94.         if (*(String+EndOfLastLine+Width-1)!=' ')
  95.         {
  96.           for (i=EndOfLastLine+Width-1;i>=EndOfLastLine;i--)
  97.             if (*(String+i)==' ')
  98.               break;
  99.  
  100.           if (i<EndOfLastLine || *(String+i)!=' ')
  101.             i=EndOfLastLine+Width-1;
  102.         }
  103.         else
  104.           i=EndOfLastLine+Width-1;
  105.  
  106.         if (EndOfLastLine+Width-1>strlen(String))
  107.           i=EndOfLastLine+(strlen(String)-EndOfLastLine)-1;
  108.  
  109.         if (OutputY==Height-1 && *(String+i+1))
  110.         {
  111.           for (;i<EndOfLastLine+Width;i++)
  112.             if (!*(String+i))
  113.               break;
  114.           if (*(String+i))
  115.             *(String+i)=0;
  116.           if (Relative>EndOfLastLine+Width-1)
  117.             Relative=EndOfLastLine+Width-1;
  118.         }
  119.  
  120.         if (Line==OutputY)
  121.         {
  122.           Start=EndOfLastLine;
  123.           End=i;
  124.           break;
  125.         }
  126.  
  127.         StrWidth-=((i-EndOfLastLine)+1);
  128.         EndOfLastLine=i+1;
  129.         OutputY++;
  130.       }
  131.     }
  132.     while (StrWidth>=0 && OutputY<Height);
  133.   }
  134. }
  135.  
  136. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  137. //
  138. // Display()
  139. //
  140. // Display MLEE - accepts variable colors
  141. //
  142. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  143.  
  144. void DiaMulti::Display(int Color)
  145. {
  146.   CurX=0;
  147.   CurY=0;
  148.  
  149.   MouseHide();
  150.  
  151.   (*Blaze) << Color;
  152.  
  153.   for (int i=0;i<Height;i++)
  154.   {
  155.     Triangulate(i);
  156.  
  157.     if (!Start && !End)
  158.       Blaze->CharacterRepeater(X,Y+i,Width,Color,' ');
  159.     else
  160.     {
  161.       for (int j=Start;j<End;j++)
  162.         (*Blaze) (X+(j-Start),Y+i) << *(String+j);
  163.       if (End-Start<Width)
  164.         for (j;j<End+(Width-(End-Start));j++)
  165.           (*Blaze) (X+(j-Start),Y+i) << ' ';
  166.       if (Relative>=Start && Relative<=End)
  167.       {
  168.         CurY=i;
  169.         CurX=Relative-Start;
  170.       }
  171.     }
  172.   }
  173.  
  174.   if (!CurX && !CurY && Relative)
  175.   {
  176.     for (i=Height-1;i>=0;i--)
  177.     {
  178.       Triangulate(i);
  179.       if (End)
  180.       {
  181.         CurY=i;
  182.         CurX=End-Start;
  183.         break;
  184.       }
  185.     }
  186.   }
  187.  
  188.   Blaze->WindowGotoXY(X+CurX,Y+CurY);
  189.  
  190.   MouseShow();
  191. }
  192.  
  193. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  194. //
  195. // Show()
  196. //
  197. // Show the character element
  198. //
  199. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  200.  
  201. static int InsertMode=0;
  202.  
  203. void DiaMulti::Show()
  204. {
  205.   Blaze->BigCursor(0);
  206.  
  207.   if (Relative>strlen(String))
  208.     Relative=strlen(String);
  209.  
  210.   Display((Available()==CompleteEvent)?Colors.CharNormal:Colors.DiaDeadLocator);
  211. }
  212.  
  213. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  214. //
  215. // HighLight()
  216. //
  217. // Highlight the character element
  218. //
  219. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  220.  
  221. void DiaMulti::HighLight()
  222. {
  223.   Blaze->BigCursor(InsertMode);
  224.  
  225.   if (Relative>strlen(String))
  226.     Relative=strlen(String);
  227.  
  228.   Display(Colors.CharHiLite);
  229. }
  230.  
  231. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  232. //
  233. // EventHandler()
  234. //
  235. // Handles the events
  236. //
  237. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  238.  
  239. int DiaMulti::EventHandler(int Event)
  240. {
  241.   switch(Event)
  242.   {
  243.     case kbBackSpace:
  244.       if (!Relative)
  245.         return CompleteEvent;
  246.  
  247.       Relative--;
  248.  
  249.     case kbDel:
  250.       if (Event!=kbBackSpace && !*(String+Relative))
  251.         return CompleteEvent;
  252.  
  253.       if (Relative<strlen(String)-1)
  254.         movmem(String+Relative+1,String+Relative,strlen(String)-Relative);
  255.       else
  256.         *(String+Relative)=0;
  257.  
  258.       HighLight();
  259.  
  260.       return CompleteEvent;
  261.  
  262.     case kbLeft:
  263.       if (Relative)
  264.       {
  265.         Relative--;
  266.         HighLight();
  267.       }
  268.  
  269.       return CompleteEvent;
  270.  
  271.     case kbRight:
  272.       if (Relative<strlen(String))
  273.       {
  274.         Relative++;
  275.         HighLight();
  276.       }
  277.  
  278.       return CompleteEvent;
  279.  
  280.     case kbUp:
  281.       if (CurY)
  282.       {
  283.         Triangulate(CurY);
  284.         int Calc=Relative-Start;
  285.         Triangulate(CurY-1);
  286.         int NewPos=Start+Calc;
  287.         if (NewPos>=Start && NewPos<=End)
  288.         {
  289.           Relative=NewPos;
  290.           HighLight();
  291.         }
  292.       }
  293.  
  294.       return CompleteEvent;
  295.  
  296.     case kbDown:
  297.       if (CurY!=Height-1)
  298.       {
  299.         Triangulate(CurY);
  300.         int Calc=Relative-Start;
  301.         Triangulate(CurY+1);
  302.  
  303.         if (!Start && !End)
  304.           return CompleteEvent;
  305.  
  306.         int NewPos=Start+Calc;
  307.         if (NewPos>=Start && NewPos<=End)
  308.         {
  309.           Relative=NewPos;
  310.           HighLight();
  311.         }
  312.       }
  313.  
  314.       return CompleteEvent;
  315.  
  316.     case kbHome:
  317.       Relative=0;
  318.       HighLight();
  319.  
  320.       return CompleteEvent;
  321.  
  322.     case kbEnd:
  323.       Relative=strlen(String);
  324.       HighLight();
  325.  
  326.       return CompleteEvent;
  327.  
  328.     case kbCtrlLeftArrow:
  329.       if (Relative)
  330.       {
  331.         int SpareRelative=Relative;
  332.  
  333.         if (*(String+SpareRelative)!=' ')
  334.         {
  335.           if (SpareRelative && *(String+SpareRelative-1)==' ')
  336.           {
  337.             SpareRelative--;
  338.             while (SpareRelative && *(String+SpareRelative)==' ')
  339.               SpareRelative--;
  340.           }
  341.           while (SpareRelative && *(String+SpareRelative)!=' ')
  342.             SpareRelative--;
  343.           if (SpareRelative)
  344.             SpareRelative++;
  345.           Relative=SpareRelative;
  346.         }
  347.         else
  348.         {
  349.           while (SpareRelative && *(String+SpareRelative)==' ')
  350.             SpareRelative--;
  351.           while (SpareRelative && *(String+SpareRelative)!=' ')
  352.             SpareRelative--;
  353.           if (SpareRelative)
  354.             SpareRelative++;
  355.           Relative=SpareRelative;
  356.         }
  357.  
  358.         HighLight();
  359.       }
  360.  
  361.       return CompleteEvent;
  362.  
  363.     case kbCtrlRightArrow:
  364.       if (*(String+Relative))
  365.       {
  366.         int SpareRelative=Relative;
  367.  
  368.         if (*(String+SpareRelative)!=' ')
  369.         {
  370.           if (*(String+SpareRelative) && *(String+SpareRelative+1)==' ')
  371.           {
  372.             SpareRelative++;
  373.             while (*(String+SpareRelative) && *(String+SpareRelative)==' ')
  374.               SpareRelative++;
  375.           }
  376.           while (*(String+SpareRelative) && *(String+SpareRelative)!=' ')
  377.             SpareRelative++;
  378.           if (*(String+SpareRelative))
  379.             SpareRelative++;
  380.           Relative=SpareRelative;
  381.         }
  382.         else
  383.         {
  384.           while (*(String+SpareRelative) && *(String+SpareRelative)==' ')
  385.             SpareRelative++;
  386.           while (*(String+SpareRelative) && *(String+SpareRelative)!=' ')
  387.             SpareRelative++;
  388.           if (SpareRelative)
  389.             SpareRelative++;
  390.           Relative=SpareRelative;
  391.         }
  392.  
  393.         HighLight();
  394.       }
  395.  
  396.       return CompleteEvent;
  397.  
  398.     case kbCtrlY:
  399.       if (strlen(String))
  400.       {
  401.         Triangulate(CurY);
  402.         movmem((String+End),(String+Start),strlen(String)-End+1);
  403.         if (Relative>strlen(String)-1)
  404.           Relative=strlen(String)-1;
  405.         else
  406.         {
  407.           Triangulate(CurY);
  408.           Relative=Start;
  409.         }
  410.  
  411.         HighLight();
  412.       }
  413.  
  414.       return CompleteEvent;
  415.  
  416.     case kbCtrlT:
  417.       if (*(String+Relative))
  418.       {
  419.         int SpareRelative=Relative;
  420.  
  421.         while (SpareRelative && *(String+SpareRelative)!=' ')
  422.           SpareRelative--;
  423.  
  424.         if (!SpareRelative)
  425.           SpareRelative--;
  426.  
  427.         Relative=SpareRelative+1;
  428.  
  429.         int End=SpareRelative+1;
  430.         while (*(String+End) && *(String+End)!=' ')
  431.           End++;
  432.  
  433.         if (*(String+End))
  434.           End++;
  435.  
  436.         movmem((String+End),(String+SpareRelative+1),strlen(String)-End+1);
  437.  
  438.         HighLight();
  439.       }
  440.  
  441.       return CompleteEvent;
  442.  
  443.     case kbIns:
  444.       InsertMode=(InsertMode)?0:1;
  445.       Blaze->BigCursor(InsertMode);
  446.       return CompleteEvent;
  447.  
  448.     case ValidatedMousedEvent:
  449.       if (MouseEvent&MouseDoubleClick)
  450.         return ClickEvent;
  451.       if (MouseEvent&MouseLeftButtonRelease)
  452.       {
  453.         MouseVertical-=Y;
  454.         MouseHorizontal-=X;
  455.  
  456.         Triangulate(MouseVertical);
  457.  
  458.         if (!Start && !End)
  459.           return CompleteEvent;
  460.  
  461.         if ((MouseHorizontal<End-Start) ||
  462.           (!*(String+End-Start) && MouseHorizontal==End-Start))
  463.         {
  464.           Relative=Start+MouseHorizontal;
  465.           HighLight();
  466.         }
  467.       }
  468.  
  469.       return CompleteEvent;
  470.  
  471.     default:
  472.       if (Event>=' ' && Event<='~')
  473.       {
  474.         if (!*(String+Relative))
  475.         {
  476.           *(String+Relative++)=Event;
  477.           *(String+Relative)=0;
  478.           HighLight();
  479.           return CompleteEvent;
  480.         }
  481.  
  482.         if (!InsertMode)
  483.         {
  484.           *(String+Relative++)=Event;
  485.           HighLight();
  486.           return CompleteEvent;
  487.         }
  488.         else
  489.         {
  490.           movmem((String+Relative),(String+Relative+1),
  491.             strlen(String)-Relative+1);
  492.           *(String+Relative++)=Event;
  493.           HighLight();
  494.           return CompleteEvent;
  495.         }
  496.       }
  497.   }
  498.   return Event;
  499. }
  500.